package com.cdh.vod.controller;

import com.cdh.base.DIYException;
import com.cdh.utils.result.Restful;
import com.cdh.utils.result.ResultCodeEnum;
import com.cdh.vod.service.VodService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Api(tags = "阿里云视频点播")
@RestController
@RequestMapping("vod")
@CrossOrigin
@Slf4j
public class VodController {

    @Autowired
    private VodService vodService;

    @PostMapping("upload")
    public Restful uploadVideo(MultipartFile file) {
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
            log.info(inputStream.toString());
            String originalFilename = file.getOriginalFilename();
            log.info(originalFilename);
            String videoId = vodService.uploadVideo(inputStream, originalFilename);
            log.info(videoId);
            return Restful.ok().data("videoId", videoId);
        } catch (IOException e) {
            throw new DIYException(ResultCodeEnum.FILE_UPLOAD_ERROR.getCode(), ResultCodeEnum.FILE_UPLOAD_ERROR.getMessage());
        }
    }

    @DeleteMapping("remove/{videoId}")
    public Restful removeVideo(@ApiParam(value = "阿里云视频id",required=true)
                               @PathVariable String videoId){
        try {
            vodService.removeVideo(videoId);
            return Restful.ok().message("视频删除成功");
        } catch (Exception e) {
            throw new DIYException(ResultCodeEnum.FILE_DELETE_ERROR.getCode(), ResultCodeEnum.FILE_DELETE_ERROR.getMessage());
        }
    }

    @DeleteMapping("removeBatch")
    public Restful removeBatch(@ApiParam(value = "阿里云视频视频列表",required=true)
                                   @RequestBody List<String> videoList){
        try {
            vodService.removeBatchByVideoList(videoList);
            return Restful.ok().message("视频删除成功");
        } catch (Exception e) {
            throw new DIYException(ResultCodeEnum.FILE_DELETE_ERROR.getCode(), ResultCodeEnum.FILE_DELETE_ERROR.getMessage());
        }
    }
}
